package edu.whut.mall.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import edu.whut.mall.admin.service.IFileManageAdminService;
import edu.whut.mall.common.enums.FileType;
import edu.whut.mall.pojo.mapper.FileMapper;
import edu.whut.mall.pojo.model.File;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * @Author smq
 * @Date 2021/8/17 16:23
 * @Uint d9lab
 * @Description:
 */
@Service
@Slf4j
public class FileManageAdminServiceImpl implements IFileManageAdminService {

    @Resource
    private FileMapper fileMapper;


    @Override
    public File addOneFile(@NotNull(message = "url不能为null") String url) {
        return addOneFile(url, FileType.图片);
    }

    @Override
    public File addOneFile(@NotNull(message = "url不能为null") String url, FileType fileType) {
        LambdaQueryWrapper<File> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(File::getUrl,url);
        File tempFile = fileMapper.selectOne(queryWrapper);
        if (tempFile != null) {
            return tempFile;
        }
        File file = new File(url, new Date(), false, fileType);
        fileMapper.insert(file);
        return file;
    }
}
